﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;
using Zwms.Client.CommonUI;

namespace Zwms.Client.ManageWms
{
    public partial class GoodsStockSelForm : BaseForm
    {
        private PagerIndex _pagerIndex;
        private StoGoodsStock _queryRecord;
        private StoGoodsStock _selectedRecord;
        private BilCountBillDetail _insertRecord;
        private IList<StoGoodsStock> _stockList;
        private IList<StoGoodsStock> _queryResultList;
        private IList<StoGoodsStock> _subList;
        public StoGoodsStock SelectedRecord { get { return _selectedRecord; } }
        public IList<StoGoodsStock> StockList { get { return _stockList; } }

        public GoodsStockSelForm(bool isSingleSelect)
        {
            InitializeComponent();
            _pagerIndex = new PagerIndex(1, int.MaxValue);
            _queryRecord = new StoGoodsStock();
            this._subList = new List<StoGoodsStock>();
            this._stockList = new List<StoGoodsStock>();
            if (isSingleSelect == true)
            {
                this.SetSigleSelState();
            }
            this.ResetDataPager();
            this.InitButtonImage();
            this.LoadStockDetail(this._queryRecord);
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void InitButtonImage()
        {
            this.btnAdd.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnAddAll.Image = global::Zwms.Client.Properties.Resources.addall;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnSelCancel.Image = global::Zwms.Client.Properties.Resources.cancel2;
            this.btnSelConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }

        private void SetQueryRecord()
        {
            this._queryRecord.WarehouseCode = this.tbsWarehouse.Tag.ToString();
            this._queryRecord.AreaCode = this.tbsArea.Tag.ToString();
            this._queryRecord.LocationCode = this.tbsLocation.Tag.ToString();
            this._queryRecord.GoodsCode = this.tbGoodsCode.Text.Trim();
            this._queryRecord.GoodsName = this.tbGoodsName.Text.Trim();
            if (this.cbGoodStatus.SelectedIndex != -1)
            {
                string sGoodStatus = CommonHelper.GetDictKeyByValue(CommonDefine.DictGoodsStatus, cbGoodStatus.SelectedItem.ToString());
                if (sGoodStatus != null)
                {
                    this._queryRecord.GoodsStatus = sGoodStatus;
                }
            }
        }

        private void SetSigleSelState()
        {
            this.btnAdd.Visible = false;
            this.btnAddAll.Visible = false;
            this.btnConfirm.Visible = false;
            this.btnCancel.Visible = false;
            this.toolStripSeparator1.Visible = false;
            this.toolStripSeparator2.Visible = false;
            this.dgv.Columns[0].Visible = false;

            this.btnSelConfirm.Visible = true;
            this.btnSelCancel.Visible = true;
        }

        private void LoadSubList(int startIndex, int endIndex)
        {
            this._subList.Clear();
            if (startIndex >= this._queryResultList.Count)
                return;
            if (endIndex >= this._queryResultList.Count)
                endIndex = this._queryResultList.Count - 1;
            StoGoodsStock item = new StoGoodsStock();
            for (int i = startIndex; i <= endIndex; i++)
            {
                item = this._queryResultList.ElementAt(i);
                this._subList.Add(item);
            }
            
        }

        private void LoadStockDetail(StoGoodsStock record)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            
            try
            {
                this.Cursor = Cursors.WaitCursor;
                _pagerIndex.CurrentPageIndex = 1;
                this._queryResultList = client.QueryGoodsStockList(record, ref _pagerIndex);
                this.LoadSubList(0, CommonDefine.iPageSize-1);
                this.stoGoodsStockBindingSource.DataSource = null;
                this.stoGoodsStockBindingSource.DataSource = this._subList ;
                this.dataPager.RecordCount = _pagerIndex.RecordCount;
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
 
        }

        

        #region 控件函数
        private void tbsWarehouse_SelectButtonClick(object sender,EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void tbsArea_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.AreaWnd uc = new InfoWms.AreaWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择库区");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
                tbsArea.Tag = uc.SelectedRecord.AreaCode;
            }
        }

        private void tbsLocation_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.LocationWnd uc = new InfoWms.LocationWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择库位");
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsLocation.SetTextBoxContent(uc.SelectedRecord.LocationName);
                tbsLocation.Tag = uc.SelectedRecord.LocationCode;
            }
        }



        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.SetQueryRecord();
            this.LoadStockDetail(this._queryRecord);
        }



        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool msg = false;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                if ((bool)dgv.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    if (!msg)
                    {
                        if (MessageBox.Show("确定要添加所选记录？", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            msg = true;
                        }
                        else
                        {
                            return;
                        }
                    }
                    this._selectedRecord = this._subList.ElementAt(i);
                    this.StockList.Add(this._selectedRecord);
                }
                
            }
            if (msg)
            {
                MessageBox.Show("所选记录已添加！");
            }
            
        }
        

        private void btnAddAll_Click(object sender, EventArgs e)
        {
            if (this._queryResultList == null || this._queryResultList.Count == 0)
            {
                MessageBox.Show("没有任何查询结果，请重新查询!");
                return;
            }
            if (MessageBox.Show("确定要添加所有查询结果?", "确定", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (StoGoodsStock item in this._queryResultList)
                {
                    this._stockList.Add(item);
                }
                MessageBox.Show("记录已添加！");
            }
           
           
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this._stockList == null || this._stockList.Count <= 0)
            {
                MessageBox.Show("没有添加任何记录！");
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSelConfirm_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选择记录！");
                return;
            }
            else
            {
                this._selectedRecord = null;
                this._selectedRecord = this._subList[rowIndex];
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnSelCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            int startIndex = _pagerIndex.CurrentPageIndex*CommonDefine.iPageSize;
            int endIndex = startIndex + CommonDefine.iPageSize -1;
            this.LoadSubList(startIndex,endIndex);
        }
        #endregion

    

       

        
    }
}
